package com.bw.sofa_module.detail;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.LogUtils;
import com.bw.common.entity.LiveDataEntity;
import com.bw.common.entity.RoomUserEntity;
import com.bw.common.mvvm.viewmodel.BaseViewModel;
import com.bw.common.provider.LoginProvider;
import com.bw.common.utils.ToastShowUtils;
import com.bw.network_sdk.entity.BaseEntity;
import com.bw.sofa_module.detail.entity.RecommentEntity;
import com.bw.sofa_module.detail.entity.delete.DeleteEntity;
import com.bw.sofa_module.detail.entity.select.RecommentSelectEntity;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class VideoDetailViewModel extends BaseViewModel<VideoDetailModel> {
    @Autowired
    LoginProvider loginProvider;

    public int userId = -1;

    public void addComment(String commentText,
                           int height,
                           String image_url,
                           long itemId,
                           String video_url,
                           int width){
        if (userId==-1){
            return;
        }else{
            mModel.requestAddComment(commentText, height, image_url, itemId,userId, video_url, width)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(this);
        }
    }

    public void selectComment(long itemId , int pageCount ,int userId){
        mModel.requestAllCommend(itemId, pageCount, userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    protected void initData() {
        ARouter.getInstance().inject(this);
        if (!loginProvider.isLogin())
            return;
        loginProvider.getUserInfo()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    public void requestDelete(long commentId,long itemId){
        if (!loginProvider.isLogin()){
            ARouter.getInstance().build("/login/loginactivity").navigation();
            return;
        }
        mModel.requestDelete(commentId, itemId, userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    protected void onRestle(BaseEntity entity) {
        if (entity instanceof RecommentEntity){
            RecommentEntity recommentEntity = (RecommentEntity) entity;
            if (recommentEntity.status != 200){
                ToastShowUtils.toastShort("评论失败，请稍后重试");
                return;
            }else{
                ToastShowUtils.toastShort("评论成功");
                selectComment(recommentEntity.data.data.itemId,10, (int) recommentEntity.data.data.author.userId);
            }
        }else if (entity instanceof RecommentSelectEntity){
            RecommentSelectEntity recommentSelectEntity = (RecommentSelectEntity) entity;
            LogUtils.e("RecommentSelectEntity:"+recommentSelectEntity.toString());
            LiveDataEntity liveDataEntity = new LiveDataEntity();
            liveDataEntity.action = LiveDataEntity.LiveDataType.START_VIEW_MODEL;
            Map<LiveDataEntity.LiveDataType,Object> map = new HashMap<>();
            map.put(LiveDataEntity.LiveDataType.ENTITY,recommentSelectEntity);
            liveDataEntity.data = map;
            liveDataViewModel.post(liveDataEntity);
        }else if (entity instanceof DeleteEntity){
            DeleteEntity deleteEntity = (DeleteEntity) entity;
            LogUtils.e("RecommentSelectEntity:"+deleteEntity.toString());
            LiveDataEntity liveDataEntity = new LiveDataEntity();
            liveDataEntity.action = LiveDataEntity.LiveDataType.DELETE_COMMENT;
            Map<LiveDataEntity.LiveDataType,Object> map = new HashMap<>();
            map.put(LiveDataEntity.LiveDataType.ENTITY,deleteEntity);
            liveDataEntity.data = map;
            liveDataViewModel.post(liveDataEntity);
        }else if (entity instanceof RoomUserEntity){
            RoomUserEntity roomUserEntity = (RoomUserEntity) entity;
            this.userId = roomUserEntity.data.data.userId;

            LiveDataEntity liveDataEntity = new LiveDataEntity();
            liveDataEntity.action = LiveDataEntity.LiveDataType.USERID;
            Map<LiveDataEntity.LiveDataType,Object> map = new HashMap<>();
            map.put(LiveDataEntity.LiveDataType.ENTITY,roomUserEntity);
            liveDataEntity.data = map;
            liveDataViewModel.post(liveDataEntity);
        }
    }
}
